package com.projectzqjz.parttimework.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import java.util.List;

public class WXQQPhoneUtils {
    //打开微信
    public static void openWX(Context context) {
        if (CheckApkExist.checkApkExist(context, "com.tencent.mm")) {
            Intent intent = new Intent();
            ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            context.startActivity(intent);
        } else {
            NewsToastUtils.showToast(context, "未安装手微信或安装的版本不支持！");
        }
    }

    //打开QQ
    public static void openQQ(Context context, String qq) {
// 跳转之前，可以先判断手机是否安装QQ
//        if (isQQClientAvailable(context)) {
//            // 跳转到客服的QQ
//            String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + qq;
//            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//            // 跳转前先判断Uri是否存在，如果打开一个不存在的Uri，App可能会崩溃
//            if (isValidIntent(context, intent)) {
//                context.startActivity(intent);
//            }
//        } else {
//            NewsToastUtils.showToast(context, "未安装手QQ或安装的版本不支持！");
//        }
        String url3521 = "mqqwpa://im/chat?chat_type=wpa&uin=" + qq;
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url3521)));
    }

    //拨打电话
    public static void openPhone(Context context, String phone) {

        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        context.startActivity(intent);
    }

    /**
     * 判断 用户是否安装QQ客户端
     */
    private static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < pinfo.size(); i++) {
            String pn = pinfo.get(i).packageName;
            if (pn.equalsIgnoreCase("com.tencent.qqlite") || pn.equalsIgnoreCase("com.tencent.mobileqq")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断 Uri是否有效
     */
    private static boolean isValidIntent(Context context, Intent intent) {
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
        return !activities.isEmpty();
    }
}
